package com.xiaohui.weibo.controller;

import com.xiaohui.weibo.dao.UserDao;
import com.xiaohui.weibo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserDao userDao;

    @GetMapping("/login")
    public String login(){
        return "login";
    }


    @PostMapping("/login")
    public String login(String loginname, String loginpwd, HttpSession httpSession, Model model){
        User user = userDao.findByLoginNameAndLoginPwd(loginname,loginpwd);
        System.out.println("this is written by xiaohui :"+loginname+loginpwd);
        System.out.println("这是吴老师打印"+loginname+loginpwd);
        if (user != null){
            httpSession.setAttribute("cur_user",user);
            //跳转到主页
            return "redirect:/index";
        }
        //输送到页面显示错误
        model.addAttribute("err","用户名或密码错误");
        return "login";
    }

    //注册
    @GetMapping("/reg")
    public String register(){
        return "reg";
    }

    @PostMapping("reg")
    public String reg(User user){
        System.out.println("王老板修改了注册方法");
        System.out.println("吴老师先修改注册方法");
        System.out.println("register !!");
        System.out.println("王老板修改了注册方法2");
        System.out.println("王老板修改了注册方法3");
        System.out.println("chen-sp-change");
        System.out.println("123");
        userDao.save(user);
        return "reg";
    }
}
